package com.stress.testing.plugin.es.strategy;

import com.stress.testing.common.conf.AgentConfig;
import com.stress.testing.common.conf.PluginConfig;

import static com.stress.testing.common.conf.PluginConfigKey.DEFAULT_ELASTICSEARCH_INDEX_NAME_PREFIX;
import static com.stress.testing.common.conf.PluginConfigKey.PLUGIN_ELASTICSEARCH_INDEX_NAME_PREFIX_KEY;

public abstract class AbstractRequestIndexRenameStrategy implements RequestIndexRenameStrategy {
    private AgentConfig agentConfig;
    private PluginConfig pluginConfig;

    public void init(AgentConfig agentConfig, PluginConfig pluginConfig) {
        this.agentConfig = agentConfig;
        this.pluginConfig = pluginConfig;
    }

    protected String internalRenameIndex(String index) {
        String indexNamePrefix = pluginConfig.get(PLUGIN_ELASTICSEARCH_INDEX_NAME_PREFIX_KEY,
                DEFAULT_ELASTICSEARCH_INDEX_NAME_PREFIX);
        if (index == null || index.startsWith(indexNamePrefix)) {
            return index;
        }

        String newIndex = indexNamePrefix + index;
        return newIndex;
    }

    protected String[] internalRenameIndex(String[] indices) {
        if (indices == null || indices.length == 0) {
            return indices;
        }

        String[] newIndices = new String[indices.length];
        for (int i = 0; i < indices.length; i++) {
            newIndices[i] = indices[i];
        }

        return newIndices;
    }
}
